<?php

/**
    modifica la materia in orario nella visualizazione del docente
    e nel database: il lato client usa Ajax per l'update
*/

include "../../lib/start.php";

check_session();
check_permission(ADM_PERM);

header("Content-type: text/plain");

if(isset($_REQUEST['getID'])){
	$ora = $_REQUEST['ora'];
	$giorno = $_REQUEST['giorno'];
	$mat = $_REQUEST['materia'];
	$classe = $_REQUEST['classe'];
	$desc = $_REQUEST['desc'];
	$sel_id = "SELECT id FROM orario WHERE giorno = '$giorno' AND ora = $ora AND classe = $classe";
	try{
		$res_id = $db->executeQuery($sel_id);
	} catch (MySQLException $ex){
		print "ko;".$ex->getQuery().";".$ex->getMessage();
		exit;
	}
	$_id = $res_id->fetch_assoc();
	$id_ora = $_id['id'];
	$teacher = $_SESSION['__uid__'];
	if($_REQUEST['act'] == 1){
		// delete
		$del = "UPDATE orario SET materia = 999, docente = NULL, descrizione = NULL WHERE id = $id_ora";
		try{
			$r_dl = $db->execute($del);
		} catch (MySQLException $ex){
			print "ko;".$ex->getQuery().";".$ex->getMessage();
			exit;
		}
		print "ok";
		exit;
	}
}
else{
	$id_ora = $_POST['uid'];
	$mat = $_POST['mat'];
	$teacher = $_POST['teacher'];
	$desc = "";
}

$upd = "UPDATE orario SET materia = $mat, docente = $teacher, descrizione = ".field_null($desc, true)." WHERE id = $id_ora";
try{
	$db->execute($upd);
} catch (MySQLException $ex){
	print "ko;".$ex->getQuery().";".$ex->getMessage();
	exit;
}
$res = "ok";
$sel_m = "SELECT materia FROM materie WHERE id_materia = $mat";
$res_m = $db->execute($sel_m);
$m = $res_m->fetch_assoc();
$res = $res.";".$m['materia'].";".$id_ora;

print $res;
exit;

?>